package edu.scau.pro.hotel.service.impl;

import edu.scau.pro.hotel.entity.*;
import edu.scau.pro.hotel.mapper.HotelCheckinMapper;
import edu.scau.pro.hotel.mapper.HotelCustomerMapper;
import edu.scau.pro.hotel.mapper.HotelRoomMapper;
import edu.scau.pro.hotel.mapper.HotelTypeMapper;
import edu.scau.pro.hotel.service.IHotelCheckinService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;

/**
 * (HotelCheckin)表服务实现类
 *
 * @author CJW
 * @since 2022-03-21 20:06:11
 */
@Service("hotelCheckinService")
public class HotelCheckinServiceImpl implements IHotelCheckinService {
    @Resource
    private HotelCheckinMapper hotelCheckinMapper;
    @Resource
    private HotelCustomerMapper hotelCustomerMapper;
    @Resource
    private HotelRoomMapper hotelRoomMapper;
    @Resource
    private HotelTypeMapper hotelTypeMapper;

    /**
     * 通过ID查询单条数据
     *
     * @param checkinId 主键
     * @return 实例对象
     */
    @Override
    public HotelCheckin queryById(Integer checkinId) {
        return this.hotelCheckinMapper.queryById(checkinId);
    }

    /**
     * 查询指定行
     *
     * @param hotelCheckin 筛选条件
     * @return 查询结果
     */
    @Override
    public List<HotelCheckin> queryAll(HotelCheckin hotelCheckin) {
        return this.hotelCheckinMapper.queryAll(hotelCheckin);
    }

    /**
     * @ gxl
     *插入客户信息前判断客户是否存在
     *
     * @param hotelCustomer 实例对象
     * @return boolean值
     */
    @Override
    public Boolean isExist(HotelCustomer hotelCustomer) {
        String idNoGet = hotelCustomer.getIdNo();
        if(this.hotelCustomerMapper.queryCustomerByIdNo(idNoGet)!=null){
            return true;
        }
        else
            return false;
    }

    /**
     * 新增数据
     *
     * @param checkInForm 实例对象
     * @return 实例对象
     */
    @Override
    public HotelCheckin insert(CheckInForm checkInForm) {
        System.out.println(checkInForm);
        // 1、从入住表单数据封装的实体CheckInForm中获取hotelCustomer对象属性
        HotelCustomer hotelCustomer = checkInForm.getHotelCustomer();
        System.out.println(hotelCustomer);
        // 2、从CheckInForm中获取hotelCheckin对象属性
        HotelCheckin hotelCheckin = checkInForm.getHotelCheckin();
        System.out.println(hotelCheckin);
        // 3.1、插入客户信息前判断客户是否存在 @ gxl
        // 3.2、若存在则添加hotelCustomer对象到数据库中
        boolean isExist = this.isExist(hotelCustomer);
        if(!isExist){
            this.hotelCustomerMapper.insert(hotelCustomer);
        }
        // 4、从数据库获取该hotelCustomer对象的id
        int customerId = hotelCustomerMapper.queryAll(hotelCustomer).get(0).getCustomerId();
        System.out.println(customerId);
        // 5、将customerId赋值给hotelCheckin对象
        hotelCheckin.setCustomerId(customerId);
        // hotelCheckin.setCheckinStatus("已入住");
        System.out.println(hotelCheckin);
        // 6、添加hotelCheckin到数据库中
        int insertNum = this.hotelCheckinMapper.insert(hotelCheckin);
        System.out.println(insertNum);
        // 7、如果插入成功，修改房间表的房间状态、房型表的空余房间数量
        if(insertNum!=0){
            // 根据roomId返回hotelRoom实体
            HotelRoom hotelRoom = this.hotelRoomMapper.queryById(hotelCheckin.getRoomId());
            // 修改hotelRoom实体状态
            hotelRoom.setRoomStatus("占用");
            // 更新到数据库hotel_room表
            this.hotelRoomMapper.update(hotelRoom);
            // 根据typeId返回hotelType实体
            HotelType hotelType = this.hotelTypeMapper.queryById(hotelRoom.getTypeId());
            // 修改状态
            hotelType.setVacantRoomNum(hotelType.getVacantRoomNum()-1);
            // 更新到数据库hotel_type表
            this.hotelTypeMapper.update(hotelType);
            hotelCheckin.setCheckinStatus("已入住");
            hotelCheckinMapper.update(hotelCheckin);
//            System.out.println(hotelCheckin);
//            HotelCheckin hotelCheckin1 = this.hotelCheckinMapper.queryById(10);
//            hotelCheckin1.setCheckinStatus("已入住");
//            this.hotelCheckinMapper.update(hotelCheckin1);
        }
        return hotelCheckin;
    }

    /**
     * 处理入住相关时间和房费
     *
     * @param checkInForm 实例对象
     * @return 实例对象
     */
    @Override
    public CheckInForm processTimeAndMoney(CheckInForm checkInForm){
        // 从CheckInForm中获取hotelCheckin对象属性
        HotelCheckin hotelCheckin = checkInForm.getHotelCheckin();
        //计算预计离开时间
        Calendar calendar = new GregorianCalendar();
        calendar.setTime(hotelCheckin.getCheckinTime());
        calendar.add(calendar.DATE,hotelCheckin.getReserveDay()); //把日期往后增加预住天数,整数  往后推,负数往前移动
        //设置预计离开时间
        checkInForm.getHotelCheckin().setReserveTimeLeave(calendar.getTime());
        //设置押金
        checkInForm.getHotelCheckin().setDeposit(100.0);
        // 根据roomId返回hotelRoom实体
        HotelRoom hotelRoom = this.hotelRoomMapper.queryById(hotelCheckin.getRoomId());
        // 根据typeId返回hotelType实体
        HotelType hotelType = this.hotelTypeMapper.queryById(hotelRoom.getTypeId());
        //设置房费
        double currentCheckinPrice = hotelType.getPrice() * hotelCheckin.getReserveDay();
        checkInForm.getHotelCheckin().setCheckinPrice(currentCheckinPrice);
        //设置应付金额
        double currentTotalMoney = currentCheckinPrice + 100;
        checkInForm.getHotelCheckin().setTotalMoney(currentTotalMoney);

        return checkInForm;
    }


    /**
     * 修改数据
     *
     * @param hotelCheckin 实例对象
     * @return 实例对象
     */
    @Override
    public HotelCheckin update(HotelCheckin hotelCheckin) {
        this.hotelCheckinMapper.update(hotelCheckin);
        return this.queryById(hotelCheckin.getCheckinId());
    }

    /**
     * 通过主键删除数据
     *
     * @param checkinId 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer checkinId) {
        return this.hotelCheckinMapper.deleteById(checkinId) > 0;
    }
}
